package com.ruoyi.project.user.company.service.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.company.domain.Company;
import com.ruoyi.project.user.company.mapper.CompanyMapper;
import com.ruoyi.project.user.company.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private IUserService userService;

    @Value("${my.mysql.post.hostid}")
    private Long companyId;

    @Override
    public List<Company> findAll(Company company) {
        return companyMapper.findAll(company);
    }

    @Override
    public int checkName(String companyName) {
        return companyMapper.checkName(companyName);
    }

    @Override
    public int checkEmail(String companyEmail) {
        return companyMapper.checkEmail(companyEmail);
    }

    @Override
    public int checkPhone(String companyPhone) {
        return companyMapper.checkPhone(companyPhone);
    }

    @Transactional
    @Override
    public int addCompany(Company company, String password) {
        Date date = new Date();
        User user = new User();
        user.setPassword(password);
        user.setEmail(company.getCompanyEmail());
        user.setPhonenumber(company.getCompanyPhone());
        user.setLoginName(company.getCompanyPhone());
        user.setUserName(company.getCompanyPhone());
        user.setCreateTime(date);
        Long [] postIds = {companyId};
        user.setPostIds(postIds);
        int index = userService.insertUser(user);
        if(index>0){
            company.setCompanyUserId(user.getUserId());
            company.setCompanyDate(date);
            int i = companyMapper.addCompany(company);
            if(i>0){
                return i;
            }
        }
        return 0;
    }

    @Transactional
    @Override
    public int updateState(Integer companyState, Integer companyId) {
        Company company = companyMapper.findCompanyById(companyId);
        //设置user登录状态
        User user = new User();
        user.setUserId(company.getCompanyUserId());
        //帐号状态（0正常 1停用）
        user.setStatus(companyState==2?"1":"0");
        user.setUpdateBy(ShiroUtils.getLoginName());
        int i = userService.updateUserInfo(user);
        if(i>0){
            return companyMapper.updateState(companyState, companyId);
        }
        return 0;
    }

    @Override
    public Company findCompanyById(Integer companyId) {
        return companyMapper.findCompanyById(companyId);
    }

    @Transactional
    @Override
    public int updateCompany(Company company) {
        Date date = new Date();
        User user = new User();
        Company companyById = companyMapper.findCompanyById(company.getCompanyId());
        user.setUserId(companyById.getCompanyUserId());
        user.setEmail(company.getCompanyEmail());
        user.setPhonenumber(company.getCompanyPhone());
        user.setLoginName(company.getCompanyPhone());
        user.setUserName(company.getCompanyPhone());
        user.setUpdateTime(date);
        int i = userService.updateUserInfo(user);
        if(i>0){
            int i1 = companyMapper.updateCompany(company);
            if(i1>0){
                return i1;
            }
        }

        return 0;
    }

    @Transactional
    @Override
    public int removeCompany(Integer[] ids) {
        int count = 0;
        for(Integer id:ids){
            Company companyById = companyMapper.findCompanyById(id);
            count +=userService.deleteUserById(companyById.getCompanyUserId());
        }
        if(count == ids.length){
            int i = companyMapper.removeCompany(ids);
            return i;
        }
        return 0;
    }

    @Override
    public List<Company> export(Company company) {
        return companyMapper.findAll(company);
    }
}
